package com.itheima.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.tanhua.api.UserInfoApi;
import com.itheima.tanhua.domain.UserInfo;
import com.itheima.tanhua.dto.RecommendUserDto;
import com.itheima.tanhua.dubbo.mappers.UserInfoMapper;
import com.itheima.tanhua.mongo.RecommendUser;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Lff
 * @date 2022/3/24
 * 用户具体信息
 */
@DubboService
//@Service
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 保存用户详情信息
     *
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 修改用户详情信息
     *
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 根据id查询记录
     *
     * @param userID
     * @return
     */
    @Override
    public UserInfo findById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    /**
     * 分页查询记录
     *
     * @param items
     * @param dto
     * @return
     */

    @Override
    public Page<UserInfo> getUserToPage(List<RecommendUser> items, RecommendUserDto dto) {
        //构建分页参数 当前页码，分页记录条数
        Page<UserInfo> userInfoPage = new Page<>(dto.getPage(), dto.getPagesize());
        userInfoMapper.findUserInfosToPage(userInfoPage, items, dto);
        return userInfoPage;
    }

    /**
     * 根据id批量查询userInfo
     *
     * @param ids
     * @param condition
     * @return
     */
    @Override
    public Map<Long, UserInfo> getUserInfosToMap(List<Long> ids, UserInfo condition) {
        LambdaQueryWrapper<UserInfo> qw = Wrappers.lambdaQuery(UserInfo.class);
        if (condition != null) {
            qw.in(ids != null && ids.size() > 0, UserInfo::getId, ids)
                    .like(StringUtils.isNotEmpty(condition.getNickname()),
                            UserInfo::getNickname, condition.getNickname())
                    .eq(StringUtils.isNotEmpty(condition.getGender()),
                            UserInfo::getGender, condition.getGender())
                    .eq(StringUtils.isNotEmpty(condition.getEducation()),
                            UserInfo::getEducation, condition.getEducation())
                    .le(condition.getAge() != null && condition.getAge() >= 0,
                            UserInfo::getAge, condition.getAge());
        }
        List<UserInfo> userInfos = userInfoMapper.selectList(qw);
        //使用stream流转成map
        Map<Long, UserInfo> resultMap = //参数一 userInfo的id 作key 参数二 代表当前元素本省 userInfo 作value
                userInfos.stream().collect(Collectors.toMap(UserInfo::getId, Function.identity()));
        return resultMap;
    }

    /**
     * 根据环信id查询用户详情信息
     *
     * @param hxUser
     * @return
     */
    @Override
    public UserInfo findUserInfoByHxUser(String hxUser) {
        UserInfo userInfo = userInfoMapper.findByHxUser(hxUser);
        return userInfo;
    }

    /**
     * 分页查询UserInfo
     *
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public Page<UserInfo> findUserInfoByPage(Integer page, Integer pagesize) {
        Page<UserInfo> pages = new Page<>(page, pagesize);
        userInfoMapper.selectPage(pages, null);
        return pages;
    }

    @Override
    public List<UserInfo> findByIds(List<Long> uids) {
        LambdaQueryWrapper<UserInfo> qw = Wrappers.lambdaQuery(UserInfo.class);
        qw.in(UserInfo::getId, uids);
        return userInfoMapper.selectList(qw);
    }
}
